﻿@namespace Masa.Tsc.Web.Admin.Rcl.Components

<div class="@Class full-height">
    <MCard Class="d-flex flex-column rounded-lg full-height">
        <PanelTooltip Title="@Value.Title" Description="@Value.Description" />
        <AutoResize Delay="10" OnResize="arg=> _height = arg.height">
            <MCardText Class="flex-grow-1">
                <MSimpleTable Height="_height-35" FixedHeader>
                    @if (Value.ShowTableHeader)
                    {
                        <thead>
                            <tr>
                                <th class="text-left">
                                    @GetFirstHeaderName()
                                </th>
                                @foreach (var field in Value.Metrics)
                                {
                                    <th class="text-left" style="@(string.IsNullOrEmpty(field.Color)?"":$"color:{field.Color} !important;")">
                                        @(string.IsNullOrWhiteSpace(field.Unit) ? field.DisplayName : $"{field.DisplayName}({field.Unit})")
                                    </th>
                                }
                            </tr>
                        </thead>
                    }
                    <tbody>
                        @{
                            var options = Value.GetTableOption();
                            if (Value.EnablePaginaton) options = options.Skip((_page - 1) * _pageSize).Take(_pageSize).ToList();
                        }
                        @foreach (var option in options)
                        {
                            <tr>
                                @for (int i = 0; i < option.Count; i++)
                                {
                                    var item = option[i];
                                    @if (item.JumpId is not null)
                                    {
                                        <td @onclick="() => ToDashboardConfiguration(item)" style="@GetTextColorStyle(i)" class="@($"text-{Value.ColumnAlignment}") primary--text @(ConfigurationRecord.IsEdit?"":"hover-pointer")">
                                            @item.Text
                                        </td>
                                    }
                                    else
                                    {
                                        <td style="@GetTextColorStyle(i)" class="@($"text-{Value.ColumnAlignment}")">@item.Text</td>
                                    }
                                }
                            </tr>
                        }
                    </tbody>
                </MSimpleTable>
                @if (Value.EnablePaginaton)
                {
                    <SPagination Class="mt-1" @bind-Page="Page" @bind-PageSize=PageSize Total=Value.GetTableOption().Count() />
                }
            </MCardText>
        </AutoResize>
    </MCard>
</div>

@code {
    int _page = 1;
    int _pageSize = 10;
    double _height = 0;
    ListTypes oldListType;

    int Page
    {
        get => _page;
        set => _page = value;
    }

    int PageSize
    {
        get => _pageSize;
        set
        {
            _page = 1;
            _pageSize = value;
        }
    }

    [CascadingParameter]
    ConfigurationRecord ConfigurationRecord { get; set; }

    [Parameter]
    public ITablePanelValue Value { get; set; }

    [Parameter]
    public string Class { get; set; }

    [Inject]
    public NavigationManager NavigationManager { get; set; }

    protected override void OnParametersSet()
    {
        if (oldListType != Value.ListType)
        {
            _page = 1;
        }
    }

    string GetTextColorStyle(int index)
    {
        if (index == 0) return "";
        var metric = Value.Metrics[index - 1];
        return string.IsNullOrEmpty(metric.Color) ? "" : $"color:{metric.Color} !important;";
    }

    string GetFirstHeaderName()
    {
        return Value.ListType switch
        {
            ListTypes.ServiceList => "Service",
            ListTypes.InstanceList => "Instance",
            ListTypes.EndpointList => "Endpoint",
            _ => ""
        };
    }

    void ToDashboardConfiguration(Dessert dessert)
    {
        if (ConfigurationRecord.IsEdit) return;
        var dashboard = dessert.JumpId;
        if (Value.ListType is ListTypes.ServiceList)
        {
            var service = dessert.Text;
            ConfigurationRecord.Service = service;
            NavigationManager.NavigateToDashboardConfiguration(dessert.JumpId, service);
        }
        else if (Value.ListType is ListTypes.InstanceList)
        {
            var instance = dessert.Text;
            ConfigurationRecord.Instance = instance;
            NavigationManager.NavigateToDashboardConfiguration(dashboard, ConfigurationRecord.Service, instance);
        }
        else if (Value.ListType is ListTypes.EndpointList)
        {
            var endpoint = dessert.Text;
            ConfigurationRecord.Endpoint = endpoint;
            NavigationManager.NavigateToDashboardConfiguration(dashboard, ConfigurationRecord.Service, ConfigurationRecord.Instance, endpoint);
        }
    }
}
